package com.caotv.wkreport.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.caotv.wkreport.dao.Bookingroom;
import com.caotv.wkreport.dao.Rooms;
import com.caotv.wkreport.dao.User;
import com.caotv.wkreport.dbutils.BookingRoomTableTool;
import com.caotv.wkreport.dbutils.RoomsTableTool;
import com.caotv.wkreport.utils.BookingMatrix;
import com.caotv.wkreport.utils.CommonUtils;
import com.caotv.wkreport.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

public class BookingRoomAction extends ActionSupport {

	private int roomId;
	private Date startDate;
	private int startHour;
	private int startMin;
	private Date endDate;
	private int endHour;
	private int endMin;
	private String description;
	private List<Rooms> listRooms = new ArrayList<Rooms>();
	private List<Integer> listHours = new ArrayList<Integer>();
	private List<Integer> listMins = new ArrayList<Integer>();
	private List<Bookingroom> listBookingRoom = new ArrayList<Bookingroom>();
	private List<BookingMatrix> listMatrix = new ArrayList<BookingMatrix>();

	public String execute() {

		startDate.setHours(startHour);
		startDate.setMinutes(startMin);
		startDate.setSeconds(0);

		endDate.setHours(endHour);
		endDate.setMinutes(endMin);
		endDate.setSeconds(0);

		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("loggedInUser");
		Bookingroom bookingroom = new Bookingroom();
		bookingroom.setUser(user);
		bookingroom.setStartTime(startDate);
		bookingroom.setEndTime(endDate);
		Rooms room = RoomsTableTool.getInstance().getRoomByID(roomId);
		bookingroom.setRooms(room);
		bookingroom.setStatus(true);
		if (BookingRoomTableTool.getInstance().addBookingRoom(bookingroom)) {
			return "success";
		} else {
			return "failed";
		}
	}

	private void initData() {
		listRooms = RoomsTableTool.getInstance().getAllRooms();
		if (startDate != null) {
			listBookingRoom = BookingRoomTableTool.getInstance()
					.getBookingRoomInfoByDate(startDate);
			listMatrix = CommonUtils.fillBookingMatrix(listRooms,
					listBookingRoom, startDate);
		}
		for (int i = 9; i < 19; i++) {
			listHours.add(i);
		}
		for (int i = 0; i < 60; i += 30) {
			listMins.add(i);
		}
	}

	public void validate() {
		initData();
		if (roomId == -1) {
			addFieldError("roomId", getText("errors.roomReq"));
		} else if (StringUtils.isNullOrEmpty(startDate)) {
			addFieldError("startDate", getText("errors.startDateReq"));
		} else if (roomId == 0) {
			addFieldError("roomId", getText("errors.roomReq"));
		} else if (StringUtils.isNullOrEmpty(endDate)) {
			addFieldError("endDate", getText("errors.endDateReq"));
		} else {
			Date today = new Date();
			today = Calendar.getInstance().getTime();
			System.out.print("Today is:" + today.toString());

			startDate.setHours(0);
			startDate.setMinutes(0);
			startDate.setSeconds(0);

			endDate.setHours(0);
			endDate.setMinutes(0);
			endDate.setSeconds(0);

			if (startDate.getTime() != endDate.getTime()) {
				addFieldError("endDate", getText("errors.sameDate"));
			}

			startDate.setHours(startHour);
			startDate.setMinutes(startMin);
			startDate.setSeconds(0);

			endDate.setHours(endHour);
			endDate.setMinutes(endMin);
			endDate.setSeconds(0);

			if (startDate.before(today)) {
				addFieldError("startDate", getText("errors.startDateAfter"));
			} else if (endDate.getTime() <= startDate.getTime()) {
				addActionError(getText("errors.endDateAfter"));
			} else {
				if (!CommonUtils.checkBookedRoom(startDate, endDate, roomId)) {
					addActionError(getText("errors.roomBooked"));
				}
			}
		}
	}

	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Rooms> getListRooms() {
		return listRooms;
	}

	public void setListRooms(List<Rooms> listRooms) {
		this.listRooms = listRooms;
	}

	public List<Bookingroom> getListBookingRoom() {
		return listBookingRoom;
	}

	public void setListBookingRoom(List<Bookingroom> listBookingRoom) {
		this.listBookingRoom = listBookingRoom;
	}

	public int getStartHour() {
		return startHour;
	}

	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	public int getStartMin() {
		return startMin;
	}

	public void setStartMin(int startMin) {
		this.startMin = startMin;
	}

	public int getEndHour() {
		return endHour;
	}

	public void setEndHour(int endHour) {
		this.endHour = endHour;
	}

	public int getEndMin() {
		return endMin;
	}

	public void setEndMin(int endMin) {
		this.endMin = endMin;
	}

	public List<Integer> getListHours() {
		return listHours;
	}

	public void setListHours(List<Integer> listHours) {
		this.listHours = listHours;
	}

	public List<Integer> getListMins() {
		return listMins;
	}

	public void setListMins(List<Integer> listMins) {
		this.listMins = listMins;
	}

	public List<BookingMatrix> getListMatrix() {
		return listMatrix;
	}

	public void setListMatrix(List<BookingMatrix> listMatrix) {
		this.listMatrix = listMatrix;
	}

}
